package moltencore.network.protocol;

import spark.network.protocol.Base;
import spark.network.Util;
import moltencore.game.Player;

public class PlayerJoinedGame extends Base {
	public static final short TYPE = 0x57;
	private static final byte[] TYPE_BYTES = Util.makeBytes(TYPE);

	private PlayerData playerData;
	private static final int PLAYER_DATA_OFFSET = Base.SIZE;

	private static final int SIZE =
			Base.SIZE;

	public PlayerJoinedGame() {
		super(true, TYPE_BYTES);
		playerData = new PlayerData();
	}

	public PlayerJoinedGame(Player player) {
		super(true, TYPE_BYTES);
		playerData = new PlayerData(player);
	}

	public PlayerData getPlayerData() {
		return playerData;
	}

	public void setPlayerData(PlayerData playerData) {
		this.playerData = playerData;
	}

	@Override
	public void getBytes(byte[] buffer) {
		super.getBytes(buffer);
		playerData.getBytes(buffer, PLAYER_DATA_OFFSET);
	}

	@Override
	public void fill(byte[] buffer) {
		super.fill(buffer);
		playerData.fill(buffer, PLAYER_DATA_OFFSET);
	}

	@Override
	public int getSize() {
		return SIZE + playerData.getSize();
	}

	public static PlayerJoinedGame create(byte[] buffer, long receiveTime) {
		PlayerJoinedGame packet = new PlayerJoinedGame();
		packet.fill(buffer);
		return packet;
	}
}